package com.smm.appdata;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;

import org.json.JSONException;
import org.json.JSONObject;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

public class AppData {

	private ArrayList<JSONObject> jsonStore = new ArrayList<JSONObject>();
	public boolean isDataLoadSuccessful = false;
	public boolean isDataWriteSuccessful = false;
	public HashMap<String, ArrayList<String>> appFilesData;
	public HashMap<String, ArrayList<JSONObject>> appSearchData;
	public HashMap<String, ArrayList<JSONObject>> appMoviesData;
	
	
	public AppData() throws JSONException {
		try {
			File f = new File(getClass().getResource("/com/smm/resource/data.json").getPath());
			if(f.exists()) {
				Gson gson = new Gson();
				BufferedReader br = new BufferedReader(new FileReader(f));
				if(br != null){
				jsonStore = gson.fromJson(br,  new TypeToken<ArrayList<JSONObject>>(){}.getType());
				}
				br.close();
				isDataLoadSuccessful =true;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	public void store(){
		try {
			Gson gson = new Gson();
			File f = new File(getClass().getResource("/com/smm/resource/data.json").getPath());
			BufferedWriter writer = new BufferedWriter(new FileWriter(f));
			writer.write(gson.toJson(jsonStore));
			writer.close();
			isDataWriteSuccessful = true;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}		 
	}


}
